package main

func smallestString(s string) string {
	firstA := -1
	firstNonA := -1
	lastA := len(s)
	allA := true
	for i, c := range s {
		if c == 'a' {
			if firstA == -1 {
				firstA = i
			} 
			lastA = i
		} else {
			if firstNonA == -1 {
				firstNonA = i
			}
			allA = false
		}
	}
	result := make([]byte, 0, len(s))
	if allA {
		result = append(result, s[:len(s)-1]...)
		result = append(result, 'z')
	} else if firstA == -1 {
		// no 'a', convert all
		for _, c := range s {
			c := byte(c)
			if c == 'a' {
				result = append(result, 'z')
			} else {
				result = append(result, c-1)
			}
		}
	} else if firstA == 0 {
		// convert s[1:]
		result = append(result, s[0:firstNonA]...)
		for _, c := range s[firstNonA:lastA] {
			c := byte(c)
			if c == 'a' {
				result = append(result, 'z')
			} else {
				result = append(result, c-1)
			}
		}
		result = append(result, s[lastA:]...)
	} else {
		// convert s[:firstA] 
		for _, c := range s[:firstA] {
			c := byte(c)
			if c == 'a' {
				result = append(result, 'z')
			} else {
				result = append(result, c-1)
			}
		}
		result = append(result, s[firstA:]...)
	}
	return string(result)
}

func main() {
	println(smallestString("aba"))
	println(smallestString("acbbc"))
	println(smallestString("leetcode"))
}